# 防盗链 - 抓取梨视频

import requests


if __name__ == '__main__':
    # 1. 拿到 contId
    url = 'https://pearvideo.com/video_1679537'
    contId = url.split('_')[1]
    print(contId)

    # 2. 拿到 videoStatus 返回的 json -> srcURL
    videoStatus = f'https://pearvideo.com/videoStatus.jsp?contId={contId}&mrd=0.7631135027610461'
    heads = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/110.0.0.0 Safari/537.36',
        # 防盗链：溯源，本次请求的上一个请求是谁
        'Referer': f'https://pearvideo.com/video_{contId}'
    }

    resp = requests.get(videoStatus, headers=heads)
    resp.close()

    print(resp.text)

    # 3. srcURL 里面的内容进行修正
    dic = resp.json()
    srcURL = dic['videoInfo']['videos']['srcUrl']
    systemTime = dic['systemTime']
    srcURL = srcURL.replace(systemTime, 'cont-' + contId)

    print(srcURL)

    # 4. 下载视频
    with open(f'../爬虫文件/{contId}.mp4', mode='wb') as file:
        file.write(requests.get(srcURL).content)
        file.close()











